using System;
using System.Windows.Forms;
using Alerter.DataSet;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using E4RPTools;

namespace Alerter.Controls
{
    public partial class ContactXtraForm : XtraForm
    {
        private long _documentId;
        private Controller _controller;
        private ContactoDataSet _dataSet;

        #region Properties

        public long DocumentId
        {
            set { _documentId = value; }
        }

        public Controller Controller
        {
            set { _controller = value; }
        }

        #endregion

        public ContactXtraForm()
        {
            InitializeComponent();
            Load += ContactXtraFormLoad;
            okSimpleButton.Click += OkSimpleButtonClick;
        }
        
        #region Private Methods

        private void SetupOperatorLookupData()
        {
            operadoraDataSet.Operadora.AddOperadoraRow(-1, "");
            operadoraDataSet.Merge(_controller.GetOperators());
            operatorLookUpEdit.Properties.Columns.Clear();
            operatorLookUpEdit.Properties.Columns.AddRange(new[] {
                new LookUpColumnInfo("Id", "Id", 81, FormatType.None, "", false, HorzAlignment.Near),
                new LookUpColumnInfo("Nome", "Nome", 85, FormatType.None, "", true, HorzAlignment.Near)});
            operatorLookUpEdit.Properties.DataSource = operadoraDataSet.Operadora;
            operatorLookUpEdit.Properties.DisplayMember = operadoraDataSet.Operadora.NomeColumn.ColumnName;
            operatorLookUpEdit.Properties.ValueMember = operadoraDataSet.Operadora.IdColumn.ColumnName;
        }

        private void MergeData()
        {
            _dataSet = new ContactoDataSet();
            if (_documentId <= 0)
            {
                var contact = _dataSet.Contacto.NewContactoRow();
                contact.Id = _documentId;
                contact.Nome = "";
                contact.Telemovel = "";
                _dataSet.Contacto.AddContactoRow(contact);
            }
            else
                _dataSet.Merge(_controller.GetContact(_documentId));
        }

        private void GetDataFromControls()
        {
            _dataSet.Contacto[0].Nome = nameTextEdit.Text;
            _dataSet.Contacto[0].Telemovel = mobileTextEdit.Text;
            if (phoneTextEdit.Text == "") _dataSet.Contacto[0].SetTelefoneNull(); else _dataSet.Contacto[0].Telefone = phoneTextEdit.Text;
            if (operatorLookUpEdit.EditValue == null || Convert.ToInt64(operatorLookUpEdit.EditValue) == -1) 
                _dataSet.Contacto[0].SetIdOperadoraNull(); 
            else
                _dataSet.Contacto[0].IdOperadora = Convert.ToInt64(operatorLookUpEdit.EditValue);
            if (emailTextEdit.Text == "") _dataSet.Contacto[0].SetEmailNull(); else _dataSet.Contacto[0].Email = emailTextEdit.Text;
            if (localeTextEdit.Text == "") _dataSet.Contacto[0].SetLocalidadeNull(); else _dataSet.Contacto[0].Localidade = localeTextEdit.Text;
        }

        private void SetDataToControls()
        {
            nameTextEdit.Text = _dataSet.Contacto[0].Nome;
            mobileTextEdit.Text = _dataSet.Contacto[0].IsTelemovelNull() ? "" : _dataSet.Contacto[0].Telemovel;
            phoneTextEdit.Text = _dataSet.Contacto[0].IsTelefoneNull() ? "" : _dataSet.Contacto[0].Telefone;
            if (_dataSet.Contacto[0].IsIdOperadoraNull()) operatorLookUpEdit.EditValue = null; else operatorLookUpEdit.EditValue = _dataSet.Contacto[0].IdOperadora;
            emailTextEdit.Text = _dataSet.Contacto[0].IsEmailNull() ? "" : _dataSet.Contacto[0].Email;
            localeTextEdit.Text = _dataSet.Contacto[0].IsLocalidadeNull() ? "" : _dataSet.Contacto[0].Localidade;
        }

        private void ClearControlErrors()
        {
            dxErrorProvider.SetError(nameTextEdit, "");
            dxErrorProvider.SetError(mobileTextEdit, "");
            dxErrorProvider.SetError(phoneTextEdit, "");
            dxErrorProvider.SetError(localeTextEdit, "");
            dxErrorProvider.SetError(emailTextEdit, "");            
        }

        private Control GetControl(string reference)
        {
            switch (reference)
            {
                case "Nome":
                    return nameTextEdit;
                case "Telemovel":
                    return mobileTextEdit;
                case "Telefone":
                    return phoneTextEdit;
                case "IdOperadora":
                    return operatorLookUpEdit;
                case "Email":
                    return emailTextEdit;
                case "Localidade":
                    return localeTextEdit;
                default:
                    return null;
            }
        }

        private void SetControlErrors(Result result)
        {
            foreach (var error in result.Errors)
                dxErrorProvider.SetError(GetControl(error.Reference), error.Message);
        }

        #endregion

        void ContactXtraFormLoad(object sender, EventArgs e)
        {
            MergeData();
            SetupOperatorLookupData();
            SetDataToControls();            
        }

        void OkSimpleButtonClick(object sender, EventArgs e)
        {
            ClearControlErrors();
            GetDataFromControls();
            Result result = _controller.IsContactValid(_dataSet);
            if (result.Failed)
            {
                DialogResult = DialogResult.None;
                SetControlErrors(result);
            }
            else
                _controller.SaveContact(_dataSet);
        }
    }
}